package server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.net.ServerSocket;

public class ServerMachine extends JFrame {

	private JPanel contentPane;
	JComboBox comboPort;
	JLabel lblPortServer;
	private JButton btnOpen;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerMachine frame = new ServerMachine();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws IOException
	 */
	public ServerMachine() throws IOException {
		setTitle("Server Machine");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 100, 207, 113);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblPortServer = new JLabel("Port Server:");
		lblPortServer.setBounds(10, 14, 68, 14);
		contentPane.add(lblPortServer);
		String item[] = { "2014", "2015", "2016", "2017", "2018" };
		comboPort = new JComboBox(item);
		comboPort.setBounds(88, 11, 92, 20);
		contentPane.add(comboPort);

		btnOpen = new JButton("OPEN");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int port = Integer.parseInt(comboPort.getSelectedItem()
						.toString());
				try {
					ServerSocket server = new ServerSocket(port);
					JOptionPane.showMessageDialog(ServerMachine.this,
							"Open port: " + server.getLocalPort());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					JOptionPane.showMessageDialog(ServerMachine.this,
							"This port was using. Please choice other port!");
				}
			}
		});
		btnOpen.setBounds(88, 41, 89, 23);
		contentPane.add(btnOpen);
	}
}
